package com.lblin.tio.server;

import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.core.maintain.ChannelContextMapWithLock;
import org.tio.server.intf.ServerAioHandler;

import com.lblin.tio.common.HelloAbsAioHandler;
import com.lblin.tio.packets.HelloPacket;

public class HelloServerAioHandler extends HelloAbsAioHandler implements ServerAioHandler {
	/** 
	 * 处理消息
	 */
	@Override
	public void handler(Packet packet, ChannelContext channelContext) throws Exception {
		HelloPacket helloPacket = (HelloPacket) packet;
		byte[] body = helloPacket.getBody();
		if (body != null) {
			String str = new String(body, HelloPacket.CHARSET);
			//System.out.println("收到消息：" + str);

			HelloPacket resppacket = new HelloPacket();
			resppacket.setBody(("服务端"+channelContext.getGroupContext().getName()+"收到了你的消息，你的消息是:" + str).getBytes(HelloPacket.CHARSET));
			resppacket.setSynSeq(helloPacket.getSynSeq());
			//System.out.println("回音的消息标志"+helloPacket.getSynSeq());
			
			ChannelContextMapWithLock syns = channelContext.getGroupContext().getWaitingResps();
			syns.put(resppacket.getSynSeq(), resppacket);
			
			Aio.send(channelContext, resppacket);
		}
		return;
	}
}